package cn.com.jinhao.provider.user.service.impl;

import cn.com.jinhao.provider.user.entity.Role;
import cn.com.jinhao.provider.user.mapper.CommonMapper;
import cn.com.jinhao.provider.user.mapper.OperRoleMapper;
import cn.com.jinhao.provider.user.mapper.RoleMapper;
import cn.com.jinhao.provider.user.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


/**
 * HotLine
 * <p>Description: </p>
 *
 * @author ShaneMew
 * @version 1.0
 * @date 2019/11/28
 * @since 2019-11-28
 */

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private OperRoleMapper operRoleMapper;
    @Resource
    private CommonMapper commonMapper;

    @Override
    public Role queryByCode(String code) {
            Role role = new Role();
            role.setCode(code);
            return roleMapper.selectOne(role);
    }

    @Override
    public boolean add(Role role) {
            String code = commonMapper.getCode("ST$ROLE");
            role.setCode(StringUtils.isBlank(code) ? "0" : String.valueOf(Integer.parseInt(code) + 1));
            roleMapper.insertSelective(role);
            return true;
    }

    @Override
    public boolean update(Role role) {
            Example example = new Example(Role.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("code", role.getCode());
            roleMapper.updateByExample(role, example);
            return true;
    }

    @Override
    public boolean delete(String code) {
            Example example = new Example(Role.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("code", code);
            roleMapper.deleteByExample(example);
            return true;
    }

    @Override
    public List<Role> queryRoles(String userCode) {
            return roleMapper.queryRoleByUserCode(userCode);
    }
}
